//#include "TSL1401.h"
#include "HardDriver.h"
#define TSL1401_SI_H   GPIO_SetBits(GPIOB,GPIO_Pin_14)
#define TSL1401_SI_L   GPIO_ResetBits(GPIOB,GPIO_Pin_14) 
#define TSL1401_CLK_H  GPIO_SetBits(GPIOB,GPIO_Pin_13)
#define TSL1401_CLK_L  GPIO_ResetBits(GPIOB,GPIO_Pin_13) 

#define PRINT_AD        (1)       //
#define THRESHOLD       (100)     //
#define WINDOW_WIDTH    (128)     //
uint16_t pixel[128];
int16_t Pixel_1[128];
int16_t Pixel_2[128];
int16_t Pixel_3[128];
float Thres = 0.60;
uint8_t Mid_Line=64;
extern uint16_t ADC_ConvertedValue;

void  TSL1401_GPIO_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	/*开启GPIOC的外设时钟*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

	/*选择要控制的GPIOC引脚*/
	GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 );

	/*设置引脚模式为通用推挽输出*/
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

	/*设置引脚速率为50MHz */
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

	/*调用库函数，初始化GPIOC*/
	GPIO_Init(GPIOB, &GPIO_InitStructure);


}





void  TSL1401_Adc_Init(void)
{
  ADC_InitTypeDef ADC_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  //GPIO_InitTypeDef GPIO_InitStructure;

  /*开启GPIOC的外设时钟*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  /*选择要控制的GPIOC引脚*/
  GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_13 | GPIO_Pin_14);

  /*设置引脚模式为通用推挽输出*/
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  /*设置引脚速率为50MHz */
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  /*调用库函数，初始化GPIOC*/
  GPIO_Init(GPIOB, &GPIO_InitStructure);






  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_ADC2, ENABLE);   //使能ADC1通道时钟

  RCC_ADCCLKConfig(RCC_PCLK2_Div6);   //72M/6=12,ADC最大时间不能超过14M
  //PA0/1/2/3 作为模拟通道输入引脚                         
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;   //模拟输入引脚
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  ADC_DeInit(ADC2);  //将外设 ADC1 的全部寄存器重设为缺省值

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;  //ADC工作模式:ADC1和ADC2工作在独立模式
  ADC_InitStructure.ADC_ScanConvMode = DISABLE; //模数转换工作在单通道模式
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //模数转换工作在单次转换模式
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换由软件而不是外部触发启动
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;  //ADC数据右对齐
  ADC_InitStructure.ADC_NbrOfChannel = 1; //顺序进行规则转换的ADC通道的数目
  ADC_Init(ADC2, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器   


  ADC_Cmd(ADC2, ENABLE);  //使能指定的ADC1

  ADC_ResetCalibration(ADC2); //重置指定的ADC1的校准寄存器

  while (ADC_GetResetCalibrationStatus(ADC2));  //获取ADC1重置校准寄存器的状态,设置状态则等待

  ADC_StartCalibration(ADC2);   //开始指定ADC1的校准状态

  while (ADC_GetCalibrationStatus(ADC2));   //获取指定ADC1的校准程序,设置状态则等待

  ADC_SoftwareStartConvCmd(ADC2, ENABLE);   //使能指定的ADC1的软件转换启动功能

}



uint16_t TSL1401_Get_Adc(uint8_t ch)
{
  //设置指定ADC的规则组通道，设置它们的转化顺序和采样时间
  ADC_RegularChannelConfig(ADC2, ch, 1, ADC_SampleTime_55Cycles5); //ADC1,ADC通道3,规则采样顺序值为1,采样时间为239.5周期              
  ADC_SoftwareStartConvCmd(ADC2, ENABLE);   //使能指定的ADC1的软件转换启动功能  

  while (!ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC));//等待转换结束

  return ADC_GetConversionValue(ADC2);  //返回最近一次ADC1规则组的转换结果
}






void TSL1401_1_GetLine(uint16_t *pixel)
{
  uint8_t i;
  
  //¿ªÊ¼SI
 // TSL1401_SI_L;
  TSL1401_CLK_L;
 // Delay_us(5);
  TSL1401_SI_H;
  //Delay_us(5);
  TSL1401_CLK_H;
  //Delay_us(5);
  TSL1401_SI_L;
  //Delay_us(5);
  pixel[0] = TSL1401_Get_Adc(ADC_Channel_8) / 8;// (uint16_t)Get_Adc(0);
  TSL1401_CLK_L;

  for(i=1; i<128; i++)
  {
    //Delay_us(5);
    TSL1401_CLK_H;
   // Delay_us(5); 
	pixel[i] = TSL1401_Get_Adc(ADC_Channel_8) / 8;//(uint16_t)Get_Adc(0);
    TSL1401_CLK_L;
  }

   //Delay_us(5);
   TSL1401_CLK_H;
  // Delay_us(5); 
   TSL1401_CLK_L;
   //Delay_us(5); 
   TSL1401_CLK_H;
   TSL1401_CLK_L;
}
void TSL1401_2_GetLine(uint16_t *pixel)
{
	uint8_t i;

	//¿ªÊ¼SI
	TSL1401_SI_L;
	TSL1401_CLK_L;
	Delay_us(5);
	TSL1401_SI_H;
	Delay_us(5);
	TSL1401_CLK_H;
	Delay_us(5);
	TSL1401_SI_L;
	Delay_us(5);
	pixel[0] = TSL1401_Get_Adc(ADC_Channel_9) / 8;// (uint16_t)Get_Adc(0);
	TSL1401_CLK_L;

	for (i = 1; i < 128; i++)
	{
		Delay_us(5);
		TSL1401_CLK_H;
		Delay_us(5);
		pixel[i] = TSL1401_Get_Adc(ADC_Channel_9) / 8;//(uint16_t)Get_Adc(0);
		TSL1401_CLK_L;
	}

	Delay_us(5);
	TSL1401_CLK_H;
	Delay_us(2);
	TSL1401_CLK_L;
	Delay_us(5);
}

void Image_Filter_2(uint16_t Pixel[], uint16_t Pixel_1[])
{
	int i;

	for (i = 0; i < 128; i++)
	{
		Pixel_1[i] = Pixel[i];
		//Pixel_2[i]=(int)Absolute(Pixel_3[i]-Pixel_3[i-1]);
	}

	return;
}


void TSL1401_2_Binarization(uint16_t *Pixel)
{
	{
		Image_Filter_2(Pixel, Pixel_1);
		uint8_t i = 0,j=0;
		uint16_t max = 0, min = 4096;
		uint16_t evv = 0;     //线性数值平均值
		uint16_t tslp = 0;
		float Threshold;//阀值
		
		evv = evv / 128;

		for (max = Pixel_1[10], min = Pixel_1[117], i = 1; i < 122; i++)
		{
			if (min > Pixel_1[i])
				min = Pixel_1[i];
			if (max < Pixel_1[i])
				max = Pixel_1[i];
		}
		Threshold = max*Thres;
// 		CCD_Max_2 = max;
// 		CCD_Min_2 = min;
// 
// 		Threshold_3 = max * UP_DOWN_2;
// 
// 		if (max < Thres_Ratio_2)
// 		{
// 			Threshold_3 = Thres_Ratio_2;
// 		}
// 
// 		if (min > Thres_Ratio_2)
// 		{
// 			Threshold_3 = Thres_Ratio_2;
// 	j = 0;	}

		for ( i = 0; i < 128; i++)
		{
			if (Pixel[i] < Threshold)
			{
				Pixel_2[i] = 0x00;
				//if(10<i&&i<117)Pixel_3[j++] = i;
			}
			else
				Pixel_2[i] = 0xff;
		}

		

	}
}
uint8_t Line(uint16_t *Pixel)
{
	uint8_t Left_Dir_1=0, Right_Dir_1=255;
	static uint8_t Last_value;

	for (int j = 64; j <= 118; j++)//104
	{
		if (Pixel[j] == 0 && Pixel[j + 1] == 0 && Pixel[j + 2] == 0 && Pixel[j + 3] == 0)
		{
			//if (Pixel[j + 1] == 0)
			//{
				Right_Dir_1 = j;
				break;
			//}
		}
	}



	for (int i = 64; i >= 10; i--)//24
	{
		if (Pixel[i] == 0 && Pixel[i - 1] == 0 && Pixel[i - 2] == 0 && Pixel[i - 3] == 0)
		{
			//if (Pixel[i - 1] == 0)
			//{
			Left_Dir_1 = i;
			break;
			//}
		}
	}



	if (Left_Dir_1<12) 
	{
		Left_Dir_1 = 12;
	}
	if (Right_Dir_1>116)
	{
		Right_Dir_1 = 116;
	}
	



	if ((64-Left_Dir_1)>(Right_Dir_1-64))
	{
		Last_value = Right_Dir_1;
		
	}

	if ((64 - Left_Dir_1)<(Right_Dir_1 - 64))
		//if
	{
		Last_value = Left_Dir_1;
		 
	}
	return Last_value;

}

/*
void SHOW_TSL8(void)
{
	//Image_Filter(Pixel_1, Pixel);
	word i = 0, max = 0, min = 4096;
	word evv = 0, tslp = 0;

	for (i = 0; i < 128; i++)
	{
		evv += Pixel[i];
	}
	evv = evv / 128;

	for (i = 0; i < 128; i++)
	{
		if (min > Pixel[i])
			min = Pixel[i];
		if (max < Pixel[i])
			max = Pixel[i];
	}

	LCD_Set_Pos(0, 7);

	Threshold_1 = max * UP_DOWN;

	for (i = 0; i < 128; i++)
	{
		if (Pixel[i] <= Threshold_1)
			tslp = 0xfe;
		else
			tslp = 0x80;
		OLED_WrDat(tslp);
	}
	if (max < 16)
	{
		//光线太暗提示
		OLED_P6x8Str(10, 7, "Sorry,lux is low!");
	}

	int Left_Mid, Right_Mid;

	for (i = DIR_MID_OFFSET; i >= DIR_LEFT_OFFSET; i--)    //显示左边线与中线的距离
	{
		if (Image_Data[i] == 0)
		{
			if (Image_Data[i - 1] == 0)
			{
				Left_Mid = DIR_MID_OFFSET - i;
				break;
			}
		}
	}
	for (int j = DIR_MID_OFFSET; j <= DIR_RIGHT_OFFSET; j++)   //显示右边线与中线的距离
	{
		if (Image_Data[j] == 0)
		{
			if (Image_Data[j + 1] == 0)
			{
				Right_Mid = j - DIR_MID_OFFSET;
				break;
			}
		}
	}

	sprintf(txt, " L_M:%03d", Left_Mid);
	LCD_P6x8Str(0, 0, txt);
	sprintf(txt, "O_M:%03d", Old_Middle);
	LCD_P6x8Str(64, 0, txt);

	sprintf(txt, "R_M:%03d", Right_Mid);
	LCD_P6x8Str(0, 2, txt);


	sprintf(txt, "L_D_1:%03d", Left_Dir_1);
	LCD_P6x8Str(5, 4, txt);
	sprintf(txt, "R_D_1:%03d", Right_Dir_1);
	LCD_P6x8Str(69, 4, txt);

	sprintf(txt, "L_D_3:%03d", Left_Dir_3);
	LCD_P6x8Str(5, 6, txt);
	sprintf(txt, "R_D_3:%03d", Right_Dir_3);
	LCD_P6x8Str(69, 6, txt);

	
}*/